package logic

import (
	"context"
	"rpc/model/mysql"
	"rpc/model/redis"

	"rpc/goods"
	"rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsAddLogic {
	return &GoodsAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsAddLogic) GoodsAdd(in *goods.AddGoodsRequest) (*goods.AddGoodsResponse, error) {
	// todo: 添加商品

	g := mysql.GoodsZye{
		GoodsTitle:  in.GoodsTitle,
		GoodsPrice:  float64(in.GoodsPrice),
		GoodsStock:  int(in.GoodsStock),
		GoodsStatus: int(in.GoodsStatus),
		GoodsAvatar: in.GoodsAvatar,
	}

	err := g.GoodsCreate()
	if err != nil {
		logx.Error("新增失败", err)
		return nil, err
	}
	//同步虚拟库存
	redis.SyncGoodsStock(int(g.ID), g.GoodsStock)

	return &goods.AddGoodsResponse{GoodsId: int64(g.ID)}, nil
}
